#include "stdafx.h"
#include "SettingsDlg.h"
#include ".\settingsdlg.h"
//#include "imglib/io.h"

IMPLEMENT_DYNAMIC(SettingsDlg, CDialog)

SettingsDlg::SettingsDlg(settings & s, CWnd * pParent) :
	CDialog(SettingsDlg::IDD, pParent), _settings(s), _coder(s.encoder)
{
	_changed = false;
}

SettingsDlg::~SettingsDlg()
{
}

BEGIN_MESSAGE_MAP(SettingsDlg, CDialog)
	ON_BN_CLICKED(IDOK, OnBnClickedOk)
	ON_CBN_SELCHANGE(IDC_FORMAT, OnCbnSelchangeFormat)
	ON_CBN_DROPDOWN(IDC_ACTIVE_ENCODER, OnCbnDropdownActiveEncoder)
	ON_CBN_SELCHANGE(IDC_ACTIVE_ENCODER, OnCbnSelchangeActiveEncoder)
	ON_BN_CLICKED(IDC_ENCODER_ABOUT, OnBnClickedEncoderAbout)
	ON_BN_CLICKED(IDC_ENCODER_CONFIGURE, OnBnClickedEncoderConfigure)
END_MESSAGE_MAP()

void SettingsDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_FORMAT, _format_combo);
	DDX_Control(pDX, IDC_FIELD, _field_combo);
	DDX_Control(pDX, IDC_ACTIVE_ENCODER, _encoders_combo);
}

BOOL SettingsDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	_format_combo.AddString("AVI file");
	_format_combo.AddString("BMP sequence");
	_format_combo.AddString("GIF sequence");
	_format_combo.AddString("JPEG sequence");
	_format_combo.AddString("PNG sequence");
	_format_combo.AddString("TGA sequence");
	_format_combo.AddString("TIFF sequence");

	_field_combo.AddString("Off");
	_field_combo.AddString("Upper First");
	_field_combo.AddString("Lower First");

	if (_coder.is_open()) {
		_encoders_combo.AddString(_coder.info()->name.c_str());
		_encoders_combo.SetCurSel(0);
	}

	WriteControls();
	UpdateEncoderInfo();

	return TRUE;  // return TRUE unless you set the focus to a control
	// EXCEPTION: OCX PropertyBase Pages should return FALSE
}

void SettingsDlg::WriteControls()
{
	int nIndex = _format_combo.FindString(0, _settings.format.c_str());
	if (nIndex == CB_ERR)
		_format_combo.SetCurSel(1);
	else
		_format_combo.SetCurSel(nIndex);

	CheckDlgButton(IDC_ALPHA, _settings.alpha ? 1 : 0);
	_field_combo.SetCurSel(_settings.field);
}

void SettingsDlg::ReadControls()
{
	CString s;
	GetDlgItemText(IDC_FORMAT, s);
	s.MakeLower();
	int start = 0;
	_settings.format = s.Tokenize(" ", start);
	_settings.alpha = IsDlgButtonChecked(IDC_ALPHA) != 0;
	_settings.field = field_render(_field_combo.GetCurSel());
}

void SettingsDlg::OnBnClickedOk()
{
	ReadControls();

	if (_coder.is_open()) {
		_settings.encoder_info = *_coder.info();
		_settings.encoder.open(_settings.encoder_info);
		img::avi::state st;
		_coder.state(st);
		_settings.encoder.set_state(st);
	}
	else
		_settings.encoder.close();

	OnOK();
}

afx_msg void SettingsDlg::OnCbnSelchangeFormat()
{
	UpdateEncoderInfo();
}

void SettingsDlg::OnCbnDropdownActiveEncoder()
{
	if (!_all_coders.empty())
		return;

	img::avi::enum_coders(_all_coders);

	unsigned i;
	for (i = 0; i != _all_coders.size(); ++i) {
		// do not insert one string twice (here and in OnInitDialog())
		if (CB_ERR == _encoders_combo.FindString(0, _all_coders[i].name.c_str()))
			_encoders_combo.AddString(_all_coders[i].name.c_str());
	}
}

struct compare_coder_name
{
	compare_coder_name(string const & e) : etalone(e) { }
	bool operator () (img::avi::coder_info const & c) const
		{ return c.name == etalone; }
private:
	string etalone;
};

void SettingsDlg::OnCbnSelchangeActiveEncoder()
{
	CString s;
	GetDlgItemText(IDC_ACTIVE_ENCODER, s);
	img::avi::coder_infos::iterator i = std::find_if(
		_all_coders.begin(), _all_coders.end(), compare_coder_name((const char*)s));
	ASSERT(i != _all_coders.end());

	_coder.open(*i);
	_settings.read_encoder_state(_coder);

	UpdateEncoderInfo();
}

void SettingsDlg::UpdateEncoderInfo()
{
	bool avi = _format_combo.GetCurSel() == 0;
	GetDlgItem(IDOK)->EnableWindow(!avi || _encoders_combo.GetCurSel() != -1);
	_encoders_combo.EnableWindow(avi);
	GetDlgItem(IDC_ENCODER_DESC)->EnableWindow(avi);
	if (_coder.is_open()) {
		img::avi::coder_info const & info = *_coder.info();
		SetDlgItemText(IDC_ENCODER_DESC, info.description.c_str());
		GetDlgItem(IDC_ENCODER_ABOUT)->EnableWindow(avi && info.has_about_dialog);
		GetDlgItem(IDC_ENCODER_CONFIGURE)->EnableWindow(avi && info.configurable);
	}
	else {
		GetDlgItem(IDC_ENCODER_ABOUT)->EnableWindow(FALSE);
		GetDlgItem(IDC_ENCODER_CONFIGURE)->EnableWindow(FALSE);
	}
}

void SettingsDlg::OnBnClickedEncoderAbout()
{
	_coder.about(m_hWnd);
}

void SettingsDlg::OnBnClickedEncoderConfigure()
{
	_coder.configure(m_hWnd);
}
